package com.cicdi.controller;

import com.cicdi.util.Common;
import com.cicdi.util.Web3jUtil;
import com.platon.contracts.ppos.dto.resp.Node;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

/**
 * @author haypo
 * @since 2021/8/18
 */
public class DelegateController implements Initializable {
    @FXML
    public TableView<Node> nodeTable;
    @FXML
    public TableColumn<Node, String> nodeId, nodeName, rewardPer, nextRewardPer, programVersion, shares;

    public void delegateTab() {
        if (nodeTable.getItems().isEmpty()) {
            nodeId.setCellValueFactory(new PropertyValueFactory<>("nodeId"));
            nodeName.setCellValueFactory(new PropertyValueFactory<>("nodeName"));
            rewardPer.setCellValueFactory(new PropertyValueFactory<>("rewardPer"));
            nextRewardPer.setCellValueFactory(new PropertyValueFactory<>("nextRewardPer"));
            programVersion.setCellValueFactory(new PropertyValueFactory<>("programVersion"));
            shares.setCellValueFactory(new PropertyValueFactory<>("shares"));

            List<Node> nodes = Web3jUtil.getNodeList();
            ObservableList<Node> observableList = FXCollections.observableArrayList(nodes);
//        observableList.sort((o1, o2) -> o2.getSubmitBlock().compareTo(o1.getSubmitBlock()));
            nodeTable.setItems(observableList);
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Common.SCHEDULED_THREAD_POOL_EXECUTOR.scheduleWithFixedDelay(
                () -> Platform.runLater(this::delegateTab), 1, 10, TimeUnit.SECONDS);
    }
}
